package com.csu.pet.demo.domain;

import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class CartItem {

    //这一项商品
    private Item item;
    //要购买的这一项商品的数量
    private int quantity;
    //商品是否有库存
    private boolean inStock;
    //商品的总价格
    private BigDecimal total;

    public  CartItem(){

    }

    public CartItem(Item item,int quantity,BigDecimal total){
        this.item = item;
        this.quantity = quantity;
        this.total = total;
    }

    public boolean isInStock() {
        return inStock;
    }

    public void setInStock(boolean inStock) {
        this.inStock = inStock;
    }
    //返回商品的总价格
    public BigDecimal getTotal() {
        return total;
    }

    //获得该item
    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
        calculateTotal();
    }

    //返回该item的数量
    public int getQuantity() {
        return quantity;
    }
    //返回该item的数量
    public void setQuantity(int quantity) {
        this.quantity = quantity;
        calculateTotal();
    }
    //增加购买item商品的数量
    public void incrementQuantity() {
        quantity++;
        calculateTotal();
    }
    //计算该item的总价钱
    private void calculateTotal() {
        if (item != null && item.getListPrice() != null) {
            total = item.getListPrice().multiply(new BigDecimal(quantity));
        } else {
            total = null;
        }
    }
}
